<template>
	<view class="content">
		<view style="height: 100vh">
			<chat :queryCallback="getQueryCallback" :openWebview="openWebview" :openMiniProgram="openMiniProgram" />
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	export default {
		data() {
			return {
				interstitialAd: null
			}
		},
		components: {

		},
		onReady() {

		},
		onLoad() {
			// 加载插屏广告
			if (this.interstitialAd == null && wx.createInterstitialAd) {
				this.interstitialAd = wx.createInterstitialAd({
					adUnitId: 'adunit-2bed5fad5a196e06'
				})
				this.interstitialAd.onLoad(() => {
					console.log('加载了')
				})
				this.interstitialAd.onError((err) => {
					console.log('加载失败')
				})
				this.interstitialAd.onClose(() => {
					console.log('关闭了')
				})
			}
			// 初始化插件
			let plugin = requirePlugin("chatbot")
			// 初始化插件配置
			plugin.init({
				appid: "MpIqK0CqH97Df7WHNyNeZdKGUAoXVd", //小程序示例账户
				openid: this.openId, //用户的openid，必填项，可通过wx.login()获取code，然后通过后台接口获取openid
				userName: this.$store.state.user.userInfo.userNick,
				textToSpeech: false, // 语音播报
				operateCardHeight: 100,
				guideCardHeight: 50,
				history: false, // 关闭用户聊天记录
				robotHeader: 'https://res.wx.qq.com/mmspraiweb_node/dist/static/miniprogrampageImages/talk/leftHeader.png',
				userHeader: 'https://res.wx.qq.com/mmspraiweb_node/dist/static/miniprogrampageImages/talk/rightHeader.png',
				success: () => {}, //非必填
				fail: (err) => {
					console.log('错误信息：', err)
				}, //非必填
			})
		},
		onShow() {
			if (this.interstitialAd) {
				this.interstitialAd.show().catch((err) => {
					console.log('err', err)
				})
			}
		},
		// 计算属性
		computed: {
			...mapState('user', ['modalShow', 'loginState', 'openId'])
		},
		methods: {
			...mapActions('user', ['openModalAction']),
			getQueryCallback(e) {
				console.log('回到首页')
			},
			// 点击机器人回答跳转
			openWebview(e) {
				console.log('openWebview')
				let url = e.detail.weburl
				// wx.navigateTo({
				// 	url: `/pages/webviewPage/webviewPage?url=${url}`
				// })
				console.log(url)
			},
			// 点击机器人小程序跳转
			openMiniProgram(e) {
				let {
					appid,
					pagepath
				} = e.detail
				if (appid) {
					wx.navigateToMiniProgram({
						appId: appid,
						path: pagepath,
						extraData: {},
						envVersion: "",
						success(res) {
							// 打开成功
							console.log('打开成功')
						}
					});
				} else {
					wx.navigateTo({
						url: pagepath,
						fail() {
							wx.switchTab({
								url: pagepath
							});
						}
					});
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		width: 100%;
		height: 100%;
	}
</style>